package com.situ.day22.db;

import com.situ.day22.Student;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class DBTest {
    @Test
    public void test() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
       //IDB db = new MySql();
       //IDB db = new Oracle();
       //db.getConnection();
        // 只出现接口，不出现具体实现类
        // 反射+配置文件

        FileInputStream fileInputStream = new FileInputStream("src/com/situ/day22/db/db.properties");
        Properties properties = new Properties();
        properties.load(fileInputStream);
        String className = properties.getProperty("className");
        //com.situ.day22.db.MySql
        Class clazz = Class.forName(className);
        //Constructor constructor = clazz.getConstructor();
        //IDB db = (IDB) constructor.newInstance();

        //如果是new无参构造方法，可以直接clazz.newInstance()
        IDB db = (IDB) clazz.newInstance();
        db.getConnection();


        //IStudentService studentService = new StudentServiceImpl();
        //IStudentService studentService;
    }

    @Test
    public void testMethod() throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //Student student = new Student();
        //student.setName("张三");

        Class clazz = Student.class;
        Student student = (Student) clazz.newInstance();
        Method method = clazz.getMethod("setName", String.class);
        method.invoke(student, "张三");
        System.out.println(student);
    }
}
